<template>
  <div class="mod-seckill-shopAuditing">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        :class="$t('language') === 'language' ? 'en-search-form': 'zh-search-form'"
      >
        <div class="input-row">
          <el-form-item
            prop="shopName"
            label="商城名称:"
          >
            <el-input
              v-model="searchForm.shopName"
              clearable
              placeholder="商城名称"
            />
          </el-form-item>

          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('product.search') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch()"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <div class="table-con audit-shop-table">
        <el-table
          ref="auditShopRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            prop="shopName"
            label="商城名称"
            min-width="150"
          />

          <el-table-column
            align="center"
            fixed="right"
            :label="$t('publics.operating')"
            width="200"
          >
            <template #default="scope">
              <div class="">
                <!-- 查看 -->
                <div
                  class="default-btn text-btn"
                  @click="onView(scope.row)"
                >
                  {{ $t('seckill.view') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <!-- 查看 -->
    <el-dialog
      v-model="visible"
      :width="800"
      :close-on-click-modal="false"
      title="查看详情"
    >
      <el-form
        ref="dataFormRef"
        :model="dataMember"
        @submit.prevent
      >
        <el-form-item
          :label="$t('marketing.applicableProductType') + ':'"
          prop="suitableProdType"
        >
          <el-radio-group
            v-model="dataMember.suitableProdType"
            disabled
          >
            <el-radio :label="0">
              {{ $t("marketing.allProdsPar") }}
            </el-radio>
            <el-radio :label="1">
              {{ $t("marketing.participateInD") }}
            </el-radio>
            <el-radio :label="2">
              {{ $t("marketing.specifiedProduct") }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 适用商品类型 -->
        <el-form-item
          v-if="dataMember.suitableProdType !== 0 && dataMember.discountProds.length"
          :label="' '"
        >
          <div class="prod-card">
            <div
              v-for="(discountProd, index) in dataMember.discountProds"
              :key="index"
            >
              <el-card
                :body-style="{ padding: '0px' }"
                style="height: 160px; width: 120px; margin-bottom: 15px;margin-right: 10px;"
              >
                <prod-pic
                  height="104px"
                  width="100%"
                  :pic="discountProd.pic"
                />
                <div class="card-prod-bottom">
                  <span class="card-prod-name">{{ discountProd.prodName }}</span>
                </div>
              </el-card>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import { isAuth } from '@/utils/index.js'

const searchForm = reactive({})

onMounted(() => {
  getDataList()
})

let tempSearchForm = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dateRange = ref([])
const dateRange2 = ref([])
let theParams = null // 保存上次点击查询的请求条件
const dataList = ref([])
const getDataList = (pageParam, newData = false) => {
  pageParam = pageParam || page
  if (newData || !tempSearchForm) {
    tempSearchForm = {
      current: pageParam.currentPage,
      size: pageParam.pageSize
    }
    theParams = JSON.parse(JSON.stringify(searchForm))
  } else {
    tempSearchForm.current = pageParam.currentPage
    tempSearchForm.size = pageParam.pageSize
  }
  theParams.discountRule = 4
  http({
    url: http.adornUrl('/platform/discount/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, theParams))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    page.currentPage = data.current

    // 末尾页数据为空重新请求
    if (!dataList.value.length && page.currentPage > 1) {
      page.currentPage = 1
      getDataList()
    }
  })
}

// 清空自定义数据
const searchReset = () => {
  dateRange.value = []
  dateRange2.value = []
}

const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const auditShopRef = ref(null)
const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value.resetFields()
  searchReset()
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}

const visible = ref(false)
const dataMember = ref({})
const onView = (row) => {
  http({
    url: http.adornUrl(`/platform/discount/info/${row.discountId}`),
    method: 'get',
    params: http.adornParams()
  }).then(({ data }) => {
    dataMember.value = data
    visible.value = true
  })
}
</script>

<style lang="scss" scoped>
.table-con {
  position: relative;
}

:deep(.export-load) {
  top: -50% !important;
}
:deep(.el-table__fixed-right::before) {
  z-index: 0;
}

.prod-card {
  display: flex;
  flex-wrap: wrap;
}
</style>
